package com.webex.ta.hydra.gui.wm;

import com.webex.ta.hydra.gui.HydraIdeFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-20
 * Time: 14:07:22
 */
public class TitlePanel extends JPanel {
    private static final Color CNT_COLOR = new Color(184, 184, 184);
    private static final Color BND_COLOR = CNT_COLOR;

    public static final Color CNT_ACTIVE_COLOR = new Color(105, 128, 180);
    public static final Color BND_ACTIVE_COLOR = CNT_ACTIVE_COLOR.brighter();

    public final static Color ACTIVE_SIDE_BUTTON_BG = new Color(179, 197, 231);
    public final static Color INACTIVE_SIDE_BUTTON_BG = new Color(200, 200, 200);
    public static final int STRUT = 0;

//    private final Icon mySeparatorActive = HydraUtils.getImage("separator.png");
//    private final Icon mySeparatorInactive = HydraUtils.getImage("inactiveSeparator.png");

    private boolean active = false;
    private final InternalDecorator dec;

    public TitlePanel(InternalDecorator dec) {
        super(new BorderLayout());
        this.dec = dec;
        this.setOpaque(false);
        DragDetector dd = new DragDetector();
        this.addMouseMotionListener(dd);
        this.addMouseListener(dd);
    }

    public void addTitle(JComponent c) {
        add(c, BorderLayout.CENTER);
    }

    public void addButtons(JComponent buttons) {
        final JPanel container = new JPanel(new BorderLayout());
        container.setOpaque(false);
        container.add(buttons, BorderLayout.CENTER);
        add(container, BorderLayout.EAST);
    }

    public void setActive(final boolean active) {
        if (this.active = active)
            return;

        this.active = active;
        repaint();
    }

    protected final void paintComponent(final Graphics g) {
        super.paintComponent(g);
        final Graphics2D g2d = (Graphics2D) g;

        Color bndColor = active ? BND_ACTIVE_COLOR : BND_COLOR;
        Color cntColor = active ? CNT_ACTIVE_COLOR : CNT_COLOR;

        g2d.setPaint(new GradientPaint(0, STRUT, bndColor, 0, getHeight(), cntColor));
        g2d.fillRect(0, STRUT, getWidth(), getHeight());
    }

    private class DragDetector extends MouseAdapter {
        private Point lastPt = null;

        @Override
        public void mouseDragged(MouseEvent e) {
            if (lastPt == null) return;

            final Window win = SwingUtilities.windowForComponent(TitlePanel.this);

            if (win instanceof HydraIdeFrame) return;

            final Rectangle oldBounds = win.getBounds();
            final Point newPoint = e.getPoint();
            SwingUtilities.convertPointToScreen(newPoint, TitlePanel.this);
            final Point offset = new Point(newPoint.x - lastPt.x, newPoint.y - lastPt.y);
            win.setLocation(oldBounds.x + offset.x, oldBounds.y + offset.y);
            lastPt = newPoint;
            if (dec.getWindowInfo().isFloating()) {
                Rectangle bounds = dec.getWindowInfo().getFloatingBounds();
                bounds.setLocation(oldBounds.x + offset.x, oldBounds.y + offset.y);
                dec.getWindowInfo().setFloatingBounds(bounds);
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            lastPt = e.getPoint();
            SwingUtilities.convertPointToScreen(lastPt, TitlePanel.this);
            dec.setActive(true);
            dec.requestFocus();
        }
    }
}
